package utils.common;

import com.google.common.base.CaseFormat;
import constant.table.TableConstact;
import entity.config.JdbcGeneration;
import entity.table.TableDto;
import enums.table.TableTypeEnum;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {
    //数据库地址
    private static String url = "";
    private static String name = "";
    private static String username = "";
    private static String password = "";

    /*public static void main(String args[]) {
        List<TableDto> list = JdbcUtil.DBManager("SELECT table_name, column_name, data_type, column_comment FROM information_schema. COLUMNS WHERE table_name in ( 't_user' )");
        System.out.println(list);
    }*/

    public static void config(JdbcGeneration jdbc) {
        url = jdbc.getConnectionURL();
        name = jdbc.getDriverClass();
        username = jdbc.getUserId();
        password = jdbc.getPassword();
    }


    public static List<TableDto> DBManager(String sql){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<TableDto> list = new ArrayList<>();

        try{
            Class.forName(name);
            // 连接诶数据库
            connection = DriverManager.getConnection(url, username, password);
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            // 读取数据
            while(resultSet.next()){
                list.add(createTableDto(resultSet));
            }

            return list;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
                if (null != preparedStatement) {
                    preparedStatement.close();
                }
                if (null != resultSet) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 创建 TableDto 对象
     */
    private static TableDto createTableDto(ResultSet resultSet) throws Exception {
        String columnNameSql = resultSet.getString(TableConstact.COLUMN_NAME);
        String dataTypeSql = resultSet.getString(TableConstact.DATA_TYPE);

        TableTypeEnum tableTypeEnum = TableTypeEnum.getEnumByDataType(dataTypeSql);
        if (tableTypeEnum == null) {
            throw new RuntimeException("类型不匹配：" + dataTypeSql);
        }

        return new TableDto(
                CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnNameSql),
                columnNameSql,
                tableTypeEnum.getClazz(),
                tableTypeEnum.getJdbcType(),
                dataTypeSql,
                resultSet.getString(TableConstact.COLUMN_COMMENT),
                resultSet.getString(TableConstact.TABLE_NAME),
                resultSet.getString(TableConstact.COLUMN_KEY)
        );
    }
}
